home *** CD-ROM | disk | FTP | other *** search
- {: Demo Program for GpTimeZone by Primoz Gabrijelcic}
- {: modified by Ferenc Szentmiklosi almasw@elender.hu}
-
- unit testUTC1;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls, Spin, Buttons,
- gpTimezone;
-
- type
- TfrmMain = class(TForm)
- lvTZ: TListView;
- grpTimezone: TGroupBox;
- Label2: TLabel;
- outStandard: TMemo;
- Label1: TLabel;
- outDaylight: TMemo;
- dateLocal: TDateTimePicker;
- timeLocal: TDateTimePicker;
- dateUTC: TDateTimePicker;
- timeUTC: TDateTimePicker;
- Label3: TLabel;
- Label4: TLabel;
- outInvalidTime: TEdit;
- btnLocD2S: TButton;
- btnLocS2D: TButton;
- dateUTC2: TDateTimePicker;
- timeUTC2: TDateTimePicker;
- dateSwatch: TDateTimePicker;
- lblSwatchHead: TLabel;
- timeSwatch: TSpinEdit;
- lblSwatch: TLabel;
- dateSwatch2: TDateTimePicker;
- lblSwatch2: TLabel;
- timeSwatch2: TSpinEdit;
- btnLocNow: TButton;
- outUTCBias: TEdit;
- Label5: TLabel;
- Label6: TLabel;
- StatusBar1: TStatusBar;
- StMonth: TSpinEdit;
- StDay: TSpinEdit;
- StandardSet: TSpeedButton;
- Label7: TLabel;
- Label8: TLabel;
- Label9: TLabel;
- DlMonth: TSpinEdit;
- Label10: TLabel;
- DlDay: TSpinEdit;
- DaylightSet: TSpeedButton;
- StaticText1: TStaticText;
- StaticText2: TStaticText;
- procedure FormCreate(Sender: TObject);
- procedure lvTZCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- procedure dateLocalChange(Sender: TObject);
- procedure btnLocD2SClick(Sender: TObject);
- procedure timeUTC2Change(Sender: TObject);
- procedure lvTZClick(Sender: TObject);
- procedure StandardSetClick(Sender: TObject);
- procedure DaylightSetClick(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure lvTZSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- private
- UTC2 : TDateTime;
- Swatch2 : TDateTime;
- changing: boolean;
- initDate: boolean;
- RegTZ : TGpRegistryTimeZones;
- HomeTZ : TTimeZoneInformation;
- procedure ShowHint(Sender: TObject);
- procedure LoadTimeZones;
- procedure UpdateZoneTimes;
- public
- end;
-
- var
- frmMain: TfrmMain;
- LocalTime : TDateTime;
- UtcTime : TDateTime;
- LocalID : integer;
- LocalSaving:boolean;
-
- implementation
-
- {$R *.DFM}
-
- const
- CSubName = 0;
- CSubH = 1;
- CSubStdB = 2;
- CSubDltB = 3;
- CSubS = 4;
- CSubDT = 5;
-
- procedure TfrmMain.ShowHint(Sender: TObject);
- begin
- StatusBar1.SimpleText := Application.Hint;
- end; { TfrmMain.ShowHint }
-
- procedure TfrmMain.FormCreate(Sender: TObject);
- begin
- Application.OnHint := ShowHint;
- initDate := true;
- RegTZ := TGpRegistryTimeZones.Create;
- LoadTimeZones;
- end; { TfrmMain.FormCreate }
-
- procedure TfrmMain.UpdateZoneTimes;
- var i:integer;
- begin
- LocalTime:=now;
- if LocalSaving then
- UtcTime:=LocalTime+(strtoint(lvTZ.Items[LocalID].Subitems[CSubDltB])/MINUTESPERDAY)
- else
- UtcTime:=LocalTime+(strtoint(lvTZ.Items[LocalID].Subitems[CSubStdB])/MINUTESPERDAY);
- for i := 0 to lvTZ.Items.Count - 1 do begin
- if lvTZ.Items[i].Subitems[CSubS] = 'Y' then
- lvTZ.Items[i].Subitems[CSubDT]:=formatdatetime('yyyy.mm.dd hh:nn',UtcTime+-1*(strtoint(lvTZ.Items[i].Subitems[CSubDltB])/MINUTESPERDAY))
- else
- lvTZ.Items[i].Subitems[CSubDT]:=formatdatetime('yyyy.mm.dd hh:nn',UtcTime+-1*(strtoint(lvTZ.Items[i].Subitems[CSubStdB])/MINUTESPERDAY));
- end;
- end; { TfrmMain.UpdateZoneTimes }
-
- procedure TfrmMain.LoadTimeZones;
- var
- DayBias : longint;
- DaylightBias: longint;
- disp : string;
- EndDate : TDateTime;
- eng : string;
- i : integer;
- North : boolean;
- p : integer;
- sign : string;
- StandardBias: longint;
- StartDate : TDateTime;
- StdBias : longint;
- TZ : TTimeZoneInformation;
- begin
- lvTZ.Items.Clear;
- LocalTime:=now;
- UtcTime:=now;
- RegTZ.Reload;
- if GetTimeZoneInformation(HomeTZ) = DWORD($FFFFFFFF) then
- FillChar(HomeTZ,SizeOf(HomeTZ),0);
- for i := 0 to regTZ.Count-1 do begin
- with lvTZ.Items.Add do begin
- Data := pointer(RegTZ[i]);
- TZ := RegTZ[i].TimeZone;
- eng := RegTZ[i].EnglishName;
- disp := RegTZ[i].DisplayName;
- sign := '+';
- if TZ.bias = 0 then
- Caption := ''
- else begin
- if TZ.bias < 0 then
- sign := '+'
- else
- sign := '-';
- Caption := Format('%s%.2d:%.2d',[sign,Abs(TZ.bias) div 60,Abs(TZ.bias) mod 60]);
- end;
- if (disp <> '') and (disp[1] = '(') then begin
- // strip (GMT+xx:xx) prefix
- p := Pos(')',disp);
- if p > 0 then
- System.Delete(disp,1,p);
- while (disp <> '') and (disp[1] = ' ') do
- System.Delete(disp,1,1);
- end;
- Subitems.Add(eng);
- if IsEqualTZ(TZ,HomeTZ) then
- Subitems.Add('H') // home time zone
- else
- Subitems.Add('');
- if GetTZDaylightSavingInfo (TZ, StartDate, EndDate, DaylightBias, StandardBias) then begin
- StdBias := TZ.Bias + TZ.StandardBias;
- DayBias := TZ.Bias + TZ.DaylightBias;
- Subitems.Add(IntToStr(StdBias));
- Subitems.Add(IntToStr(DayBias));
- north := EndDate > StartDate;
- if north then
- if (LocalTime >= StartDate) and (LocalTime <= EndDate) then
- Subitems.Add('Y')
- else
- Subitems.Add('N')
- else if (LocalTime >= StartDate) and (LocalTime <= EndDate) then
- Subitems.Add('N')
- else
- Subitems.Add('Y');
- end
- else begin
- StdBias := TZ.Bias;
- DayBias := TZ.Bias;
- Subitems.Add(IntToStr(StdBias));
- Subitems.Add(IntToStr(DayBias));
- Subitems.Add('N');
- end;
- Subitems.Add('');
- end; //with
- end; //for
- // select home time zone
- lvTZ.Items[20].Selected := true;
- for i := 0 to lvTZ.Items.Count - 1 do begin
- if lvTZ.Items[i].Subitems[CSubH] <> '' then begin
- lvTZ.Items[i].Selected := true;
- lvTZ.Items[i].MakeVisible(false);
- LocalSaving := lvTZ.Items[i].Subitems[CSubS] = 'Y';
- LocalID:=i;
- break;
- end;
- end; //for
- UpdateZoneTimes;
- lvTZ.OnCLick(lvTZ);
- end; { TfrmMain.LoadTimeZones }
-
- procedure TfrmMain.lvTZCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- var
- bias1, bias2: longint;
- begin
- bias1 := - TGpRegistryTimeZone(Item1.Data).TimeZone.Bias;
- bias2 := - TGpRegistryTimeZone(Item2.Data).TimeZone.Bias;
- if bias1 < bias2 then
- Compare := -1
- else if bias1 > bias2 then
- Compare := 1
- else
- Compare := StrIComp(PChar(Item1.Caption),PChar(Item2.Caption));
- end; { TfrmMain.lvTZCompare }
-
- procedure TfrmMain.dateLocalChange(Sender: TObject);
-
- function GetDT(dateUTC, timeuTC: TDateTimePicker): TDateTime;
- begin
- Result := Int(FixDT(dateUTC.Date))+Frac(FixDT(timeUTC.Time));
- end; { GetDT }
-
- var
- tmp : TDateTime;
- date2 : TDateTime;
- date : TDateTime;
- engName : string;
- dispName: string;
- TZ : TTimeZoneInformation;
- begin
- if not changing then begin
- changing := true;
- try
- if assigned(lvTZ.Selected) then begin
- with TGpRegistryTimeZone(lvTZ.Selected.Data) do begin
- TZ := TimeZone;
- engName := EnglishName;
- dispName := DisplayName;
- end; //with
- // Recalc UTC from Internet Time.
- if (Sender = dateSwatch) or (Sender = timeSwatch) then begin
- date := SwatchToUTC(Trunc(dateSwatch.Date),timeSwatch.Value);
- dateUTC.Date := date;
- timeUTC.Time := date;
- end;
- // Recalc Local Time from UTC.
- if (Sender = dateUTC) or (Sender = timeUTC) or
- (Sender = dateSwatch) or (Sender = timeSwatch) then
- begin
- date := UTCToTZLocalTime(TZ,GetDT(dateUTC,timeUTC));
- dateLocal.Date := date;
- timeLocal.Time := date;
- outInvalidTime.Hide; dateUTC.Show; timeUTC.Show;
- end;
- // Recalc UTC from Local Time. This will update second UTC display,
- // show Invalid Time indicator etc.
- date := TZLocalTimeToUTC(TZ,GetDT(dateLocal,timeLocal),false);
- if date <> 0 then begin // valid time
- outInvalidTime.Show;
- date2 := TZLocalTimeToUTC(TZ,GetDT(dateLocal,timeLocal),true);
- if not DateEQ(date,date2) then begin
- if DateEQ(date2,GetDT(dateUTC,timeUTC)) then begin
- tmp := date;
- date := date2;
- date2 := tmp;
- end;
- UTC2 := date2;
- dateUTC2.Date := date2;
- timeUTC2.Time := date2;
- dateUTC2.Show; timeUTC2.Show;
- end
- else
- dateUTC2.Hide; timeUTC2.Hide;
- dateUTC.Date := date;
- timeUTC.Time := date;
- dateUTC.Show; timeUTC.Show;
- end
- else begin
- dateUTC.Hide; timeUTC.Hide; dateUTC2.Hide; timeUTC2.Hide;
- outInvalidTime.Show;
- end;
- // Recalc Internet Time from all visible UTC Time controls.
- if dateUTC.Visible then begin
- timeSwatch.Value := UTCToSwatch(GetDT(dateUTC,timeUTC),date);
- dateSwatch.Date := date;
- end;
- dateSwatch.Visible := dateUTC.Visible;
- timeSwatch.Visible := dateUTC.Visible;
- lblSwatch.Visible := dateUTC.Visible;
- lblSwatchHead.Visible := dateUTC.Visible;
- if dateUTC2.Visible then begin
- timeSwatch2.Value := UTCToSwatch(GetDT(dateUTC2,timeUTC2),Swatch2);
- dateSwatch2.Date := Swatch2;
- end;
- dateSwatch2.Visible := dateUTC2.Visible;
- timeSwatch2.Visible := dateUTC2.Visible;
- lblSwatch2.Visible := dateUTC2.Visible;;
- end;
- finally changing := false end;
- end;
- end; { TfrmMain.dateLocalChange }
-
- procedure TfrmMain.btnLocD2SClick(Sender: TObject);
- var
- StdBias : longint;
- DayBias : longint;
- StdDate : TDateTime;
- DayDate : TDateTime;
- newDT : TDateTime;
- engName : string;
- dispName: string;
- TZ : TTimeZoneInformation;
- begin
- if assigned(lvTZ.Selected) then begin
- with TGpRegistryTimeZone(lvTZ.Selected.Data) do begin
- TZ := TimeZone;
- engName := EnglishName;
- dispName := DisplayName;
- end; //with
- if (Sender = btnLocNow) or
- GetTZDaylightSavingInfo (TZ, DayDate, StdDate, DayBias, StdBias) then
- begin
- if Sender = btnLocD2S then
- newDT := StdDate-(StdBias-DayBias)/MINUTESPERDAY
- else if Sender = btnLocS2D then
- newDT := DayDate
- else if Sender = btnLocNow then
- newDT := UTCToTZLocalTime(TZ,LocalTimeToUTC(Now,false))
- else
- newDT := 0;
- dateLocal.Date := newDT;
- timeLocal.Time := newDT;
- dateLocalChange(dateLocal);
- end;
- end;
- UpdateZoneTimes;
- end; { TfrmMain.btnLocD2SClick }
-
- procedure TfrmMain.timeUTC2Change(Sender: TObject);
- begin
- dateUTC2.Date := UTC2;
- timeUTC2.Time := UTC2;
- dateSwatch2.Date := Swatch2;
- end; { TfrmMain.timeUTC2Change }
-
- procedure TfrmMain.lvTZClick(Sender: TObject);
- var
- DayBias : longint;
- StdBias : longint;
- StandardBias: longint;
- DaylightBias: longint;
- EndDate : TDateTime;
- StartDate : TDateTime;
- TZ : TTimeZoneInformation;
- engName : string;
- dispName : string;
- Item : TListItem;
- const
- OrdNums: array [1..5] of string = ('1st', '2nd', '3rd', '4th', 'last');
- begin
- StMonth.Value:=1;
- StDay.Value:=1;
- DlMonth.Value:=1;
- DlDay.Value:=1;
- if lvTZ.Selected = nil then
- Exit;
- Item := lvTZ.Selected;
- // Most of this code was shamelessly stolen from Delphi Clinic,
- // The Delphi Magazine, Issue 49. It was written by Brian Long.
- with TGpRegistryTimeZone(lvTZ.Selected.Data) do begin
- TZ := TimeZone;
- engName := EnglishName;
- dispName := DisplayName;
- end; //with
- StaticText1.Caption := ' '+dispName+' ';
- if GetTZDaylightSavingInfo (TZ, StartDate, EndDate, DaylightBias, StandardBias) then
- begin
- StdBias := TZ.Bias + TZ.StandardBias;
- DayBias := TZ.Bias + TZ.DaylightBias;
- outStandard.Lines.Clear;
- outDaylight.Lines.Clear;
- outStandard.Lines.Add(Format('%s, %d minute bias',[TZ.StandardName, StdBias]));
- outDaylight.Lines.Add(Format('%s, %d minute bias',[TZ.DaylightName, DayBias]));
- StMonth.Enabled:=true;
- StDay.Enabled:=true;
- DlMonth.Enabled:=true;
- DlDay.Enabled:=true;
- StandardSet.Enabled:=true;
- DayLightSet.Enabled:=true;
- if item.Subitems[CSubS] = 'Y' then begin // just saving
- if TZ.DaylightName='' then
- StaticText2.Caption:=item.Subitems[CSubName]
- else
- StaticText2.Caption:=TZ.DaylightName;
- end
- else begin
- if TZ.StandardName='' then
- StaticText2.Caption:=item.Subitems[CSubName]
- else
- StaticText2.Caption:=TZ.StandardName;
- end;
- if TZ.StandardDate.wYear = 0 then begin //"Day of month" date
- with TZ.StandardDate do begin
- outStandard.Lines.Add(Format('Starts on %s %s of %s at %s GMT', [
- OrdNums[wDay], LongDayNames[wDayOfWeek + 1], LongMonthNames[wMonth],
- TimeToStr(EncodeTime(wHour, wMinute, wSecond, wMilliseconds) + DayBias / MINUTESPERDAY)]));
- StMonth.Value:=wMonth;
- StDay.Value:=wDay;
- end;
- outStandard.Lines.Add('This year: '+FormatDateTime('c',EndDate));
- end
- else begin //Absolute date
- StMonth.Enabled:=false;
- StDay.Enabled:=false;
- DlMonth.Enabled:=false;
- DlDay.Enabled:=false;
- StandardSet.Enabled:=false;
- DayLightSet.Enabled:=false;
- outStandard.Lines.Add('Absolute date: '+
- DateTimeToStr(SystemTimeToDateTime(TZ.StandardDate) + DayBias / MINUTESPERDAY));
- end;
- if TZ.DaylightDate.wYear = 0 then begin //"Day of month" date
- with TZ.DaylightDate do begin
- outDaylight.Lines.Add(Format('Starts on %s %s of %s at %s GMT', [
- OrdNums[wDay], LongDayNames[wDayOfWeek + 1], LongMonthNames[wMonth],
- TimeToStr(EncodeTime(wHour, wMinute, wSecond, wMilliseconds) + StdBias / MINUTESPERDAY)]));
- DlMonth.Value:=wMonth;
- DlDay.Value:=wDay;
- end;
- outDaylight.Lines.Add('This year: '+FormatDateTime('c',StartDate));
- end
- else begin //Absolute date
- StMonth.Enabled:=false;
- StDay.Enabled:=false;
- DlMonth.Enabled:=false;
- DlDay.Enabled:=false;
- StandardSet.Enabled:=false;
- DayLightSet.Enabled:=false;
- outDaylight.Lines.Add('Absolute date: '+
- DateTimeToStr(SystemTimeToDateTime(TZ.DaylightDate) + StdBias / MINUTESPERDAY));
- end;
- btnLocD2S.Enabled:=true;
- btnLocS2D.Enabled:=true;
- end
- else begin // no DST
- StMonth.Enabled:=false;
- StDay.Enabled:=false;
- DlMonth.Enabled:=false;
- DlDay.Enabled:=false;
- StandardSet.Enabled:=false;
- DayLightSet.Enabled:=false;
- outStandard.Text := Format('%s, %d minute bias',[TZ.StandardName, TZ.Bias]);
- outDaylight.Text := 'not used';
- StaticText2.Caption:=item.Subitems[CSubName];
- btnLocD2S.Enabled:=false;
- btnLocS2D.Enabled:=false;
- end;
- outUTCBias.Text := IntToStr(GetTZBias(TZ));
- outStandard.Perform(EM_SETSEL,0,0);
- outStandard.Perform(EM_SCROLLCARET,0,0);
- outDaylight.Perform(EM_SETSEL,0,0);
- outDaylight.Perform(EM_SCROLLCARET,0,0);
- if initDate then begin
- initDate := false;
- dateLocal.Date := Trunc(Now);
- timeLocal.Time := Frac(Now);
- end;
- dateLocalChange(dateLocal);
- end; { TfrmMain.lvTZClick }
-
- procedure TfrmMain.StandardSetClick(Sender: TObject);
- var
- TZ: TTimeZoneInformation;
- begin
- with TGpRegistryTimeZone(lvTZ.Selected.Data) do begin
- TZ := TimeZone;
- TZ.StandardDate.wMonth := StMonth.Value;
- TZ.StandardDate.wDay := StDay.Value;
- WriteAccess := true;
- TimeZone := TZ;
- WriteAccess := false;
- lvTZClick(self);
- end; //with
- end;
-
- procedure TfrmMain.DaylightSetClick(Sender: TObject);
- var
- TZ: TTimeZoneInformation;
- begin
- with TGpRegistryTimeZone(lvTZ.Selected.Data) do begin
- TZ := TimeZone;
- TZ.DaylightDate.wMonth := DlMonth.Value;
- TZ.DaylightDate.wDay := DlDay.Value;
- WriteAccess := true;
- TimeZone := TZ;
- WriteAccess := false;
- lvTZClick(self);
- end; //with
- end;
-
- procedure TfrmMain.FormDestroy(Sender: TObject);
- begin
- RegTZ.Free;
- RegTZ := nil;
- end;
-
- procedure TfrmMain.lvTZSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- begin
- if Selected then
- lvTZClick(Sender);
- end;
-
- end.
-